$(function () {
    var plan_datagrid = $("#plan_datagrid");
    var plan_form = $("#plan_form");
    var plan_form2 = $("#plan_form2");
    var plan_dialog = $("#plan_dialog");

    plan_datagrid.datagrid({
        url: '/employeeNeeds/query.do',
        fit: true,
        pagination: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: '#plan_tb',
        columns: [
            [
                {
                    field: 'departmentName',
                    title: '申请部门',
                    width: 100
                },
                {
                    field: 'applier',
                    title: '申请人',
                    width: 100
                },
                {
                    field: 'job',
                    title: '招聘职位',
                    width: 100
                },
                {
                    field: 'form',
                    title: '聘用形式',
                    width: 100
                },
                {
                    field: 'number', title: '人数', width: 100
                },

                {
                    field: 'education', title: '学历', width: 100
                },
                {
                    field: 'required', title: '技能要求', width: 100
                },
                {
                    field: 'salary', title: '薪资情况', width: 100
                },

                {
                    field: 'remark', title: '备注', width: 100
                },

            ]
        ],
        rownumbers: true
    })


    var methodObj = {
        reload: function () {
            plan_datagrid.datagrid('reload');
        },
        save:function() {
            plan_form.form("submit",{
                url:'/employeeNeeds/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "保存成功", 'info', function () {
                            methodObj['cancel']();
                            methodObj['reload']();
                        })
                    } else {
                        $.messager.alert("温馨提示", "保存失败", 'warning')
                    }
                }
            })
        },
        cancel: function () {
            plan_dialog.dialog('close')
            plan_datagrid.datagrid("reload")
        },

        changeState:function() {
            var row = plan_datagrid.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }
            $.messager.confirm("确认对话框","你确定要执行操作吗?",function (r) {
                if(r){
                    $.get('/employeeNeeds/changeState.do',{id:row.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示","执行成功",'info',function () {
                                methodObj['cancel']();
                                methodObj['reload']();
                            })
                        }else{
                            $.messager.alert("温馨提示","执行失败",'warning')
                        }
                    })
                }
            })
        },

        del:function () {
            var row = plan_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            $.messager.confirm("确认对话框","你确定要删除吗?",function (r) {
                if(r){
                    $.get('/employeeNeeds/delete.do',{id:row.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示","删除成功",'info',function () {
                                methodObj['cancel']();
                                methodObj['reload']();
                            })
                        }else{
                            $.messager.alert("温馨提示","删除失败",'warning')
                        }
                    })
                }
            })
        },

        add: function () {
            plan_dialog.dialog('open');
            plan_dialog.form('clear');
            plan_dialog.dialog('setTitle', '招聘计划新增');
        },
        query:function() {
            var keyword = $("[name=keyword]").val();
            var name = $("[name=name]").val();
            var beginDate = $("[name=beginDate]").val();
            var endDate = $("[name=endDate]").val();

            plan_datagrid.datagrid('reload',{
                keyword:keyword,
                name:name,
                beginDate:beginDate,
                endDate:endDate
            })
        },

    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })


    plan_dialog.dialog({
        width: 1000,
        height: 600,
        buttons: "#plan_btn2",
        closed: true,
        modal: true,
        onClose: function () {
            plan_datagrid.dialog('clear');
        }
    })


    $('#tt').edatagrid({
        url: '/employeeNeeds/selectAll.do',
        fit:true,
        fitColumns:true,
        toolbar:'#tb',
        autoSave:true,
        saveUrl: '/employeeNeeds/saveOrUpdate.do',
        updateUrl: '/employeeNeeds/saveOrUpdate.do',
        destroyMsg:{
            norecord:{
                // 在没有记录选择的时候执行
                title:'温馨提示',
                msg:'请选中一行数据'
            },
            confirm:{
                // 在选择一行的时候执行
                title:'确认框',
                msg:'你确定要删除这条数据吗?'
            }
        },
        onDestroy:function (index, row) {
            $.get('/employeeNeeds/delete.do',{id:row.id},function (data) {
                if(data.success){
                    $.messager.alert("温馨提示","执行成功",'info',function () {
                        $('#tt').edatagrid('reload');
                    })
                }else{
                    $.messager.alert("温馨提示","执行失败",'warning')
                }
            })
        },
        onAdd:function (index,row) {
            $.messager.alert("温馨提示",'已添加一行数据','info',function () {
                $.get('/employeeNeeds/saveOrUpdate.do',{id:row.id},function () {
                    $('#tt').edatagrid('reload');
                })
            })
        }


    })




})

function add1() {
    // 追加一个空行
    $('#tt').edatagrid('addRow');
}

function del() {
    // 销毁所有选择的行
    $('#tt').edatagrid('destroyRow');
    //$('#tt').edatagrid('reload');
}
function relo() {
    $('#tt').edatagrid('reload');
}